package com.ruoyi.teacher.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import com.ruoyi.teacher.domain.Teacher;
import com.ruoyi.teacher.mapper.TeacherMapper;
import com.ruoyi.teacher.service.ITeacherService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * 教师管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-17
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
        implements ITeacherService {
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);
    @Autowired
    protected Validator validator;
    @Autowired
    private TeacherMapper teacherMapper;

    /**
     * 查询教师管理
     *
     * @param id 教师管理主键
     * @return 教师管理
     */
    @Override
    public Teacher selectTeacherById(Long id) {
        return teacherMapper.selectTeacherById(id);
    }

    /**
     * 查询教师管理列表
     *
     * @param teacher 教师管理
     * @return 教师管理
     */
    @Override
    public List<Teacher> selectTeacherList(Teacher teacher) {
        return teacherMapper.selectTeacherList(teacher);
    }

    /**
     * 新增教师管理
     *
     * @param teacher 教师管理
     * @return 结果
     */
    @Override
    public int insertTeacher(Teacher teacher) {
        teacher.setCreateTime(DateUtils.getNowDate());
        return teacherMapper.insertTeacher(teacher);
    }

    /**
     * 修改教师管理
     *
     * @param teacher 教师管理
     * @return 结果
     */
    @Override
    public int updateTeacher(Teacher teacher) {
        teacher.setUpdateTime(DateUtils.getNowDate());
        return teacherMapper.updateTeacher(teacher);
    }

    /**
     * 批量删除教师管理
     *
     * @param ids 需要删除的教师管理主键
     * @return 结果
     */
    @Override
    public int deleteTeacherByIds(Long[] ids) {
        return teacherMapper.deleteTeacherByIds(ids);
    }

    /**
     * 删除教师管理信息
     *
     * @param id 教师管理主键
     * @return 结果
     */
    @Override
    public int deleteTeacherById(Long id) {
        return teacherMapper.deleteTeacherById(id);
    }

    /**
     * 导入教师数据
     *
     * @param teacherList     教师列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    public String importTeacher(List<Teacher> teacherList, Boolean isUpdateSupport, String operName) {

        if (StringUtils.isNull(teacherList) || teacherList.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Teacher teacher : teacherList) {
            try {
                // 验证是否存在这个用户
                Teacher c = teacherMapper.selectTeacherById(teacher.getId());
                if (StringUtils.isNull(c)) {
                    BeanValidators.validateWithException(validator, teacher);
                    teacher.setCreateBy(operName);
                    teacherMapper.insertTeacher(teacher);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、教师 " + teacher.getTeacherName() + " 导入成功");
                } else if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, teacher);
                    teacher.setId(c.getId());
                    teacher.setUpdateBy(operName);
                    teacherMapper.updateTeacher(teacher);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、教师 " + teacher.getTeacherName() + " 更新成功");
                } else {
                    System.out.println(3);
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、教师 " + teacher.getTeacherName() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、教师 " + teacher.getTeacherName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
